/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.border.Border;
import org.aswing.border.EmptyBorder;
import org.aswing.border.TitledBorder;
import org.aswing.BorderLayout;
import org.aswing.Component;
import org.aswing.Icon;
import org.aswing.Insets;
import org.aswing.JButton;
import org.aswing.JLabel;
import org.aswing.JPanel;
import org.aswing.JTextArea;
import org.aswing.JTextComponent;
import org.aswing.JTextField;
import org.aswing.SoftBoxLayout;

/**
 * Basic methods to help AsWing UI construction.
 *
 * @author Romain Ecarnot
 */
class fvaswing.utils.FvUIUtil
{
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Creates a new JPanel with TiledBorder
	 */
	public static  function createTiledPanel( caption : String, margin : Number ) : JPanel
	{
		var p : JPanel = new JPanel( new SoftBoxLayout( SoftBoxLayout.Y_AXIS, 5 ) );
		p.setBorder( createTiledBorder( caption, margin ) );
		return p;
	}
	
	/**
	 * Creates a tiled border.
	 */
	public static function createTiledBorder( caption : String, margin : Number ) : Border
	{
		if( margin == undefined ) margin = 10;
		return new TitledBorder( _createInternalBorder( margin ), caption, TitledBorder.TOP, TitledBorder.LEFT, margin );	
	}
	
	/**
	 * Creates and returns new <strong>JButton</strong> using passed-in arguments.
	 * 
	 * @param caption Button's label.
	 * @param icon Button's icon
	 */
	public static function createButton( caption : String, icon : Icon ) : JButton
	{
		var b : JButton = new JButton( caption, icon );
		return b;
	}
	
	/**
	 * Creates and returns a <strong>JLabel</strong> with passed-in {@code caption}
	 * 
	 * <p>Horizontal alignment is set to {@code JLabel.LEFT}
	 */
	public static function createLabel( caption : String ) : JLabel
	{
		var l : JLabel = new JLabel( caption, JLabel.LEFT );
		return l;
	}
	
	/**
	 * Creates and returns a <strong>JLabel</strong> with passed-in {@code caption}
	 * 
	 * <p>Horizontal alignment is set to {@code JLabel.LEFT}
	 */
	public static function createLabelWidth( width : Number, caption : String ) : JLabel
	{
		var l : JLabel = createLabel( caption );		l.setPreferredWidth( width );
		return l;
	}
	
	/**
	 * Creates and returns new <strong>JTextField</strong> with text change listener.
	 * 
	 * @param func Handler to call when text change
	 * @param context Handler scope
	 */
	public static function createListeneableTextField( func : Function, context ) : JTextField
	{
		var j : JTextField = new JTextField();
		j.addChangeListener( func, context );
		
		return j;	
	}
	
	/**
	 * Creates and returns new <strong>JTextArea</strong> with text change listener.
	 * 
	 * @param func Handler to call when text change
	 * @param context Handler scope
	 */
	public static function createListeneableTextArea( func : Function, context ) : JTextArea
	{
		var j : JTextArea = new JTextArea();
		j.addChangeListener( func, context );
		
		return j;	
	}
	
	/**
	 * Creates and returns new <strong>JTextField</strong> with text actions listener.
	 * 
	 * <p>Change event is broadcasted only when user press <strong>Enter</strong> button 
	 * or when textfield loose focus.
	 * 
	 * <p>Use {@link #createListeneableTextField()} method to listen text change event.
	 * 
	 * @param func Handler to call when text change
	 * @param context Handler scope
	 */
	public static function createActionListeneableTextField( func : Function, context ) : JTextField
	{
		var input : JTextField = new JTextField();
		input.addActionListener( func, context );
		input.addEventListener( JTextField.ON_FOCUS_LOST, func, context );
		
		return input;	
	}
	
	/**
	 * Creates and returns a <strong>JPanel</strong> ( BorderLayout ) with a <strong>JLabel</strong> 
	 * on {@code BorderLayout.LEFT} and a <strong>JTextComponent</strong> on {@code BorderLayout.CENTER}
	 */
	public static function createLabelInputLine( label : JLabel, input : JTextComponent ) : JPanel
	{
		var line : JPanel = new JPanel( new BorderLayout( 5,5 ) );
		line.append( label, BorderLayout.WEST );
		line.append( input, BorderLayout.CENTER ); 
		
		return line;
	}
	
	/**
	 * Creates and returns a <strong>JPanel</strong> ( BorderLayout ) with a <strong>JLabel</strong> 
	 * on {@code BorderLayout.LEFT} and a <strong>Component</strong> on {@code BorderLayout.CENTER}
	 */
	public static function createLabelComponentLine( label : JLabel, component : Component ) : JPanel
	{
		var line : JPanel = new JPanel( new BorderLayout( 5,5 ) );
		line.append( label, BorderLayout.WEST );
		line.append( component, BorderLayout.CENTER ); 
		
		return line;
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
		
	/**
	 * Creates internal border on component
	 */
	private static function _createInternalBorder( margin : Number ) : Border
	{
		return 	new EmptyBorder( null, new Insets( 0, margin, margin, margin ) );
	}
	
	/**
	 * Constructor.
	 */
	private function FvUIUtil()
	{
		
	}
}